/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Receiver;
import com.rzico.basics.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.ReceiverMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * 收货地址业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class ReceiverService extends BaseServiceImpl<Receiver, String> {

    @Autowired
    private ReceiverMapper receiverMapper;

    @Autowired
    private MemberService memberService;

    @Override
    public BaseMapper<Receiver, String> getMapper() {
        return receiverMapper;
    }

    public int save(Receiver receiver) {

        if (receiver.getIsDefault()) {
            Map<String,Object> params = new HashMap<>();
            params.put("memberId",receiver.getMemberId());
            receiverMapper.updateReceiver(params);

            Member member = memberService.selectByPrimaryKey(receiver.getMemberId());
            if (member.getAreaId()==null) {
                member.setAreaId(receiver.getAreaId());
                member.setAreaName(receiver.getAreaName());
                member.setAddress(receiver.getAddress());
                if (member.getName()==null || member.getName().indexOf("手机用户")<0) {
                    member.setName(receiver.getConsignee());
                }
                memberService.updateByPrimaryKeySelective(member);
            }

        }

        int affectCount = receiverMapper.insertUseGeneratedKeys(receiver);

        return affectCount;

    }


    public int update(Receiver receiver) {

        if (receiver.getIsDefault()) {
            Map<String,Object> params = new HashMap<>();
            params.put("memberId",receiver.getMemberId());
            receiverMapper.updateReceiver(params);

            Member member = memberService.selectByPrimaryKey(receiver.getMemberId());
            if (member.getAreaId()==null) {
                member.setAreaId(receiver.getAreaId());
                member.setAreaName(receiver.getAreaName());
                member.setAddress(receiver.getAddress());
                if (member.getName()==null || member.getName().indexOf("手机用户")<0) {
                    member.setName(receiver.getConsignee());
                }
                memberService.updateByPrimaryKeySelective(member);
            }

        }

        int affectCount = receiverMapper.updateByPrimaryKeySelective(receiver);

        return affectCount;

    }

}
